<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Back extends Admin_Controller {

	public function index()
	{
		$this->load->view("back/index");
	}
	
	
	public function user_info(){
		
		$this->load->view("back/user_info");
	}
	
	public function edit_user(){
	
		$this->load->view("back/edit_user");
	}
	
	public function edit_user_post(){
	    $name = $_POST['name']?$_POST['name']:'';
	    $email = $_POST['email']?$_POST['email']:'';
	    $password = $_POST['password']?$_POST['password']:'';
	    $password2 = $_POST['password2']?$_POST['password2']:'';
	    $old_password = $_POST['old_password']?$_POST['old_password']:'';
	    if(!$password || !$password2 || !$old_password){
	    	$this->load->view("back/edit_user",array("msg"=>"密码不能为空"));
	    	return;
	    }
	    if($password!=$password2){
	    	$this->load->view("back/edit_user",array("msg"=>"两次密码不一致"));
	    		return;
	    }
	    if(md5($old_password)!=$_SESSION['member']['password']){
	    	$this->load->view("back/edit_user",array("msg"=>"您输入的密码不对"));
	    		return;
	    }
		$data = array(
				"name"=>$name,
				"email"=>$email,
				"password"=>md5($password)
		);
		$_SESSION['member']['name']=$name;
		$_SESSION['member']['email']=$email;
		$_SESSION['member']['password']=md5($password);
		$this->load->model("Member_Model");
		$flag = $this->Member_Model->update($data,"id=".$_SESSION['member']['id']);
		if($flag){
			$this->load->view("back/user_info");
				return;
		}else{
			$this->load->view("back/edit_user",array("msg"=>"修改信息失败"));
				return;
		}
		
	}
	
	/**
	 * 获取收藏夹内容
	 */
	public function collect(){
		$project_style = $_GET['project_style']?$_GET['project_style']:1;
		$page = $_GET['page']?$_GET['page']:1;
		$this->load->model("Collection_Model");
		$collection_list = $this->Collection_Model->get_list_ad("c.deleted=0 and a.deleted=0 and a.project_style=$project_style",($page-1)*10,10);
		$total = $this->Collection_Model->getTotal("c.deleted=0 and a.deleted=0 and a.project_style=$project_style");
		$this->load->view("back/connection_list",array(
				"list"=>$collection_list,
				"page"=>$page,
				"project_style"=>$project_style,
				"page_size"=>ceil($total/10)
		));
	}
	
	public function collect_article(){
		$this->load->model("Collection_Model");
		$article_id = $_GET['article_id']?$_GET['article_id']:1;
		$member_id = $_SESSION['member']['id'];
		$data = array(
				"article_id"=>$article_id,
				"member_id"=>$member_id,
				"create_date"=>date("Y-m-d H:i:s"),
				"modify_date"=>date("Y-m-d H:i:s"),
		);
		
		$collect_info = $this->Collection_Model->get_one("deleted=0 and article_id=".$article_id." and member_id=".$_SESSION['member']['id']);
		if(!$collect_info){
			$id = $this->Collection_Model->add($data);
		}
		header("Location: /front/article_detail?id=$article_id");
	}
	
	public function cancle_collect_article(){
		$this->load->model("Collection_Model");
		$article_id = $_GET['article_id']?$_GET['article_id']:1;
		$member_id = $_SESSION['member']['id'];
		$data = array(
				"deleted"=>1,
		);
		$collect_info = $this->Collection_Model->update($data,"deleted=0 and article_id=".$article_id." and member_id=".$_SESSION['member']['id']);
		header("Location: /front/article_detail?id=$article_id");
	}
}
